package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.utils.QiniuUtils;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;

    /**
     * 图片的上传
     *
     * @param file
     * @return
     */
    @RequestMapping("upload")
    public Result upload(@RequestParam("imgFile") MultipartFile file) {
        try {
            //获取上传文件的文件名称
            String originalFilename = file.getOriginalFilename();
            //获取后缀名
            int index = originalFilename.indexOf(".");
            String suffix = originalFilename.substring(index);
            //拼接一个唯一的文件名称
            String fileName = UUID.randomUUID() + suffix;
            //将图片上传到七牛云
            QiniuUtils.upload2Qiniu(file.getBytes(), fileName);
            //在redis中保存
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    @RequestMapping("add")
    public Result add(@RequestParam("token") String token ,
                      @RequestParam("checkGroupIds") Integer[] checkgroupIds,@RequestBody Setmeal setmeal) {
        try {
            Boolean tokenExist = jedisPool.getResource().exists(token);
            if (!tokenExist){
                return new Result(false, "请求已超时");
            }

            setmealService.add(checkgroupIds, setmeal);
            //添加完成后删除reids中的token验证码
            jedisPool.getResource().del(token);
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = setmealService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);

        }
    }
}
